<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
$_helper = $this->helper('catalog/output');
$helper = $this->helper('shoppersettings/image');
$imgX = 252;
$imgY = $helper->calculateHeight($imgX);
$time = time() + rand(0,1000);
?>
<?php if($this->getItems()->getSize()): ?>
<div class="slider-container">
    <div class="clearfix">
        <h3><?php echo $this->__('Related Products') ?></h3>
        <a href="#" class="jcarousel-prev-horizontal" id="shopper_carousel_prev<?php echo $time; ?>"></a>
        <a href="#" class="jcarousel-next-horizontal" id="shopper_carousel_next<?php echo $time; ?>"></a>
        <?php /*<p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or') ?>&nbsp;<a href="#" onclick="selectAllRelated(this); return false;"><?php echo $this->__('select all') ?></a></p> */ ?>
    </div>
    <ul id="related-products-list" class="jcarousel-skin-tango clearfix">
        <?php foreach ($this->getItems() as $_product): ?>
        <li class="item clearfix">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
                <?php echo $this->helper('shoppersettings')->getLabel($_product);  ?>
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX, $imgY) ?>" data-srcX2="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX*2, $imgY*2) ?>" width="<?php echo $imgX; ?>" height="<?php echo $imgY; ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
            </a>
            <div class="button-container">
                <?php if($_product->isSaleable()): ?>
                <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                <?php else: ?>
                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
            </div>
            <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
            <a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a>
            <?php if(!$_product->isComposite() && $_product->isSaleable()): ?>
            <?php if (!$_product->getRequiredOptions()): ?>
                <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_product->getId() ?>" name="related_products[]" value="<?php echo $_product->getId() ?>" />
                <?php endif; ?>
            <?php endif; ?>
            <?php echo $this->getPriceHtml($_product, true) ?>
        </li>
        <?php endforeach; ?>
    </ul>
    <div class="clear"></div>
</div>
<script type="text/javascript">
//<![CDATA[
    jQuery(document).ready(function($) {

        function mycarousel_initCallback(carousel)
        {
            $('#shopper_carousel_next<?php echo $time; ?>').bind('click', function() {
                carousel.next();
                return false;
            });
            $('#shopper_carousel_prev<?php echo $time; ?>').bind('click', function() {
                carousel.prev();
                return false;
            });

            if (typeof $('.slider-container').swipe !== 'undefined'){
                $('.slider-container').swipe({
                    swipeLeft: function() { carousel.next(); },
                    swipeRight: function() { carousel.prev(); },
                    swipeMoving: function() {}
                });
            }
        };

        $('#related-products-list').jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback,
            buttonNextHTML: null,
            buttonPrevHTML: null
        });

        //line up carousel items
        $(window).load(function(){
            var height = 0;
            $('li.item', '#related-products-list').each(function(i,v){
                $(this).css('height', 'auto');
                if ( $(this).height() > height ) {
                    height = $(this).height();
                }
            });
            $('li.item', '#related-products-list').height(height);
        });

        $('div.main').after($('<div class="slider-container" />'));

        function carouselResize ()
        {
            var $h = $('.main div.slider-container').outerHeight();
            var $wrap = $('.main-container > .slider-container').css({
                width: '100%',
                height: $h,
                marginTop: '-'+$h+'px',
                position: 'absolute'
            });
        }
        carouselResize();
        $(window).resize(carouselResize);

    });

$$('.related-checkbox').each(function(elem){
    Event.observe(elem, 'click', addRelatedToProduct)
});

var inArray = function (arr, val) {
    var i = arr.length
    while (i--) {
        if (arr[i] === val) return true
    }
    return false
}

var relatedProductsCheckFlag = false;
function selectAllRelated(txt){
    if (relatedProductsCheckFlag == false) {
        $$('.related-checkbox').each(function(elem){
            elem.checked = true;
        });
        relatedProductsCheckFlag = true;
        txt.innerHTML="<?php echo $this->__('unselect all') ?>";
    } else {
        $$('.related-checkbox').each(function(elem){
            elem.checked = false;
        });
        relatedProductsCheckFlag = false;
        txt.innerHTML="<?php echo $this->__('select all') ?>";
    }
    addRelatedToProduct();
}

function addRelatedToProduct(){
    var checkboxes = $$('.related-checkbox');
	var allValues = [];
    var values = [];
    for(var i=0;i<checkboxes.length;i++){
        allValues.push(checkboxes[i].value);
        if(checkboxes[i].checked) values.push(checkboxes[i].value);
    }
    if($('related-products-field')){
	    var current = $('related-products-field').value.split(',');
        for (var i = 0; i < current.length; i++) {
            if ( !inArray(values, current[i]) && !inArray(allValues, current[i]) ) values.push(current[i]);
        }
        $('related-products-field').value = values.join(',');
    }
}

//]]>
</script>

<?php endif ?>